﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Security.Cryptography;
using System.Text;

namespace vngoodlife.Util
{
    public class GeneralClass
    {
        /// <summary>
        /// Encode MD5
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string GetMD5Hash(string value)
        {
            MD5 md5Hasher = MD5.Create();
            byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(value));
            StringBuilder sBuilder = new StringBuilder();
            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }
            return sBuilder.ToString();
        }


        /// <summary>
        /// Cut string by number of words
        /// </summary>
        /// <param name="oldString">String should be cut</param>
        /// <param name="numberOfWord">Number of word after cutting</param>
        /// <returns></returns>
        public static string CutStringByWords(string oldString, int numberOfWord)
        {
            string result = "";
            string suffix = "";
            string[] stringArray = oldString.Trim().Split();

            if (numberOfWord < stringArray.Length)
            {
                suffix = "...";
            }
            else
            {
                numberOfWord = stringArray.Length;
            }

            for (int i = 0; i < numberOfWord ; i++)
            {
                result += stringArray[i] + " ";
            }

            return result.Trim() + suffix;
        }

        /// <summary>
        /// Remove redundant hyphen
        /// </summary>
        /// <param name="oldString">String should be cut</param>
        /// <param name="numberOfWord">Number of word after cutting</param>
        /// <returns></returns>
        public static string RemoveRedundantHyphen(string oldString)
        {
            string result = "";
            string[] stringArray = oldString.Trim().Split('-');

            for (int i = 0; i < stringArray.Length; i++)
            {
                if (stringArray[i] != "" && i < stringArray.Length - 1)
                {
                    result += stringArray[i] + "-";
                }
                if (stringArray[i] != "" && i == stringArray.Length - 1)
                {
                    result += stringArray[i];
                }
            }
            return result.Trim();
        }


        /// <summary>
        /// Convert date in database (MM/dd/yyyy hh:mm:ss) to dd/mm/yyyy
        /// </summary>
        /// <param name="strDate">string date</param>
        /// <returns>String dd/mm/yyyy</returns>
        public static String ConvertDate_1(DateTime strDate)
        {
            try
            {
                return strDate.ToString("dd/MM/yyyy").ToString();
            }
            catch (Exception)
            {
                return "";
            }
            
        }

        /// <summary>
        /// Convert date in database (MM/dd/yyyy hh:mm:ss) to (dd/mm/yyyy hh:mm:ss)
        /// </summary>
        /// <param name="strDate">Datetime</param>
        /// <returns>String dd/mm/yyyy</returns>
        public static String ConvertDate_1B(DateTime strDate)
        {
            try
            {
                return strDate.ToString("dd/MM/yyyy HH:mm:ss").ToString();
            }
            catch (Exception)
            {
                return "";
            }
        }

        /// <summary>
        /// Convert date in database (MM/dd/yyyy hh:mm:ss) to (dd/mm/yyyy hh:mm  AM/PM)
        /// </summary>
        /// <param name="strDate">Datetime</param>
        /// <returns>String dd/mm/yyyy</returns>
        public static String ConvertDate_1C(String strDate)
        {
            try
            {
                return DateTime.Parse(strDate).ToString("dd/MM/yyyy HH:mm tt").ToString();
            }
            catch (Exception)
            {
                return "";
            }
        }

        /// <summary>
        /// Convert date in database (MM/dd/yyyy hh:mm:ss) to (hh:mm AM/PM, dd/mm/yyyy)
        /// </summary>
        /// <param name="strDate">Datetime</param>
        /// <returns>String dd/mm/yyyy</returns>
        public static String ConvertDate_1D(DateTime strDate)
        {
            try
            {
                return strDate.ToString("hh:mm tt, dd/mm/yyyy").ToString();
            }
            catch (Exception)
            {
                return "";
            }
        }


        /// <summary>
        /// Convert date (dd/MM/yyyy) or (dd/MM/yyyy hh:mm:ss GMT) to MM/dd/yyyy
        /// </summary>
        /// <param name="strDate">string date</param>
        /// <returns>String MM/dd/yyyy</returns>
        public static String ConvertDate_2(String strDate)
        {

            if (strDate != null && strDate != "")
            {
                string[] splDate = strDate.Split(new char[] { '/', ' ' });
                strDate = splDate[1] + "/" + splDate[0] + "/" + splDate[2];
            }
            return strDate;
        }


        /// <summary>
        /// cut string in description
        /// </summary>
        /// <param name="str"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public static String CutString(String str, int length)
        {
            Boolean flag = true;
            while (flag)
            {
                try
                {
                    string temp = str.Substring(str.IndexOf('<'), str.IndexOf('>') - str.IndexOf('<') + 1);
                    str = str.Replace(temp, "");
                    if (temp == "")
                        flag = false;
                }
                catch (Exception)
                {
                    flag = false;
                }
            }
            str = str.Length > length ? str.Substring(0, length).Substring(0, str.Substring(0, length).LastIndexOf(' ')) + " ..." : str;
            return str.Replace("  ", " ");
        }


        /// <summary>
        /// Replace \r\n and \t in string
        /// </summary>
        /// <param name="ck"></param>
        /// <returns></returns>
        public static string ReplaceSpecialCharacterForCK(string ck)
        {
            return ck.Replace("\r\n", "").Replace("\t", "");
        }

        /// <summary>
        /// "hom nay luc 8gio"
        /// </summary>
        /// <param name="ck"></param>
        /// <returns></returns>
        public static string BuildDateTimeString(DateTime time)
        {
            string str = "ngày " + String.Format("{0:dd/MM/yyy}", time);
            DateTime now = DateTime.Now;
            if (now.Year == time.Year)
            {
                if (now.Month == time.Month)
                {
                    if (now.Date == time.Date)
                    {
                        if (now.Hour == time.Hour && (now.Minute - time.Minute < 30))
                        {
                            str = "vài phút trước";
                        }
                        else
                        {
                            str = "Hôm nay lúc " + String.Format("{0:HH:mm}", time);
                        }
                    }

                    if (now.Day - time.Day == 1)
                    {
                        str = "Hôm qua lúc " + String.Format("{0:HH:mm}", time);
                    }

                    if (now.Day - time.Day > 1 && now.Day - time.Day <= 7)
                    {
                        string temp = "";
                        switch (time.DayOfWeek.ToString())
                        {
                            case "Monday": temp = "Thứ hai"; break;
                            case "Tuesday": temp = "Thứ ba"; break;
                            case "Wednesday": temp = "Thứ tư"; break;
                            case "Thursday": temp = "Thứ năm"; break;
                            case "Friday": temp = "Thứ sáu"; break;
                            case "Saturday": temp = "Thứ bảy"; break;
                            case "Sunday": temp = "Chủ nhật"; break;
                            default: temp = time.DayOfWeek.ToString();break;
                        }
                        str = temp + " lúc " + String.Format("{0:HH:mm}", time);
                    }
                }
            }
            return str;
        }
    }
}